package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.EmptyViewHolder;
import com.cmccgz.udacs_landlord_webapp.bean.Person;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: June
 * @Descripion:
 * @date 2017/10/30 10:29
 */

public class PersonListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Person> data;
    private OnRvItemClickListenter<Person>listenter;
    public PersonListAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return EmptyViewHolder.defaultEmptyViewHolder(parent,"暂无人员，下拉可刷新!");
            case 1:
                return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_person_list, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==1) {
            ((ViewHolder) holder).instanceView(position);
        }
    }


    @Override
    public int getItemCount() {
        return data == null ||data.size()==0? 1 : data.size();
    }

    private class ViewHolder extends BaseHolder {

        private TextView tv_username;
        private TextView tv_idcard;
        private TextView tv_house_no;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            tv_username = (TextView) findViewById(R.id.tv_username);
            tv_idcard = (TextView) findViewById(R.id.tv_idcard);
            tv_house_no = (TextView) findViewById(R.id.tv_house_no);
        }

        @Override
        public void instanceView(final int position) {
            final Person person = data.get(position);
            tv_username.setText(TextUtils.isEmpty(person.getUserName()) || "null".equals(person.getUserName()) ? "" : person.getUserName());
            tv_idcard.setText(person.getId_card());
            tv_house_no.setText(person.getHouse_no());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listenter!=null) {
                        listenter.onItemClick(person,position,itemView);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return data==null||data.size()==0?-1:1;
    }

    public void changeData(List<Person> list) {
        this.data.clear();
        if (list != null && list.size() > 0) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void appendData(List<Person> list) {
        if (list != null && list.size() > 0) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void setListenter(OnRvItemClickListenter<Person> listenter) {
        this.listenter = listenter;
    }
}
